<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/3/23
 * Time: 11:02
 */
namespace app\admin\controller;
use think\Db;
class CircleController extends CommonController
{
    /**
     *getUserData 页面加载获取用户表数据
     * @return json格式数组
     */
    public function getData(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $where = $this->getDataByCampusid($_POST);
        $res = Db::table('ew_circle')->alias('c')
            ->join('ew_u_user u','c.circle_user_id=u.user_id','LEFT')
            ->where($where)
            ->field('c.*,user_id,user_username,user_phone')
            ->order('circle_createtime DESC')
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total =Db::table('ew_circle')->alias('c')
            ->join('ew_u_user u','c.circle_user_id=u.user_id','LEFT')
            ->where($where)
            ->field('user_id')
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }


    /**
     * seeUserInfo 动态详情
     * @return 返回状态
     */
    public function seeInfo(){
        $cid = input('cid');
        $data = Db::table('ew_circle')->alias('c')
            ->join('ew_u_user u','c.circle_user_id=u.user_id','LEFT')
            ->where('circle_id',$cid)
            ->field('c.*,user_id,user_username,user_phone')
            ->find();
        if($data['circle_image']){
            $data['circle_image'] = explode(',',$data['circle_image']);
        }
        $this->assign ('data',$data);
        return $this->fetch("circle/circle_info");
    }
    /**
     * 删除评论
     * cid
     * @return string
     */
    public function delete(){
        $cid = $_POST['cid'];
        // 启动事务
        Db::startTrans();
        try{
            //拿到数据之后删掉这条动态
            Db::table('ew_circle')->where('circle_id',$cid)->delete();
            //删除掉这个动态的所有回复
            Db::table('ew_circle_message')->where('cm_circle_id',$cid)->delete();
            Db::table('ew_circle_zan')->where('cz_circle_id',$cid)->delete();
            // 提交事务
            Db::commit();
            return json_encode(['status'=>200,'msg'=>'成功']);
            //=====end===============
        } catch (\Exception $e) {
            Db::rollback();
            return json_encode(['status'=>0,'msg'=>'失败']);
        }
    }

    /**
     * 删除某动态的评论的回复
     * @return string
     */
    public function delcir() {
        $cmid = $_POST['cmid'];
        // 启动事务
        Db::startTrans();
        try{
            $data = Db::table('ew_circle_message')
                ->where('cm_id',$cmid)
                ->field('cm_circle_id')
                ->find();
            $cid = $data['cm_circle_id'];
            //删除掉这个动态的回复
            Db::table('ew_circle_message')->where('cm_id',$cmid)->delete();
            Db::table('ew_circle')
                ->where('circle_id',$cid)
                ->setDec('circle_message');
            // 提交事务
            Db::commit();
            return json_encode(['status'=>200,'msg'=>'成功']);
            //=====end===============
        } catch (\Exception $e) {
            Db::rollback();
            return json_encode(['status'=>0,'msg'=>'失败']);
        }
    }
    /**
     * 查看某用户的动态——html
     * @return string
     */
    public function myCirs() {
        $cid = $_POST['cid'];
        $this->assign ('cid',$cid);
        return $this->fetch("circle/circle_mes");
    }
    /**
     * 查看某用户的评论——数据
     * @return string
     */
    public function myCirsTable() {
        $cid = $_POST['cid'];
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $res = Db::table("ew_circle_message")->alias('cm')
            ->join('ew_u_user u','cm.cm_one_user_id=u.user_id','LEFT')
            ->join('ew_u_user u2','cm.cm_reply_user_id=u2.user_id','LEFT')
            ->field('cm.*,u.user_id one_id,u2.user_id reply_id,u.user_username one_name,u2.user_username reply_name')
            ->where('cm_circle_id',$cid)
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total = Db::table("ew_circle_message")->alias('cm')
            ->join('ew_u_user u','cm.cm_one_user_id=u.user_id','LEFT')
            ->join('ew_u_user u2','cm.cm_reply_user_id=u2.user_id','LEFT')
            ->field('u.user_id one_id')
            ->where('cm_circle_id',$cid)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

}